import play.api.libs.json.{JsValue, Json}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Promise}

val ss = Seq("1", "2")
var stringq = "1,2,3,4"
stringq.split(",")
ss.mkString(",")

val p = Promise[Int]
val f = p.future
val producer = Future {
  val r = 1 + 1
  if (r != 2)
    p failure (new IllegalStateException)
  else {
    val q = r + 1
    p success q
  }
}
producer.onSuccess({ case i => println(i) })

val a = Future {
  2 / 0
}

for (exc <- a.failed) exc

val a2 = Future {
  4 / 2
}
for (exc <- a2.failed) println(exc)

//val full: Option[Any] = JSON.parseFull("""{"id":1,"name":"hello world","data":[{"id":2,"name":"name2"}]}""")
//full match {
//  case Some(map:Map[String,Any]) =>
//    map.get("data") match {
//      case data:List[Map[String,Any]] => JSON.
//    }
//}
case class JsonModel(id: Int, name: String)

implicit val jsonModel = Json.format[JsonModel]

val json: JsValue = Json.parse("""{"id":1,"name":"hello world","data":[{"id":2,"name":"name2"},{"id":3,"name":"name3"}]}""")
(json \ "data").validate[List[JsonModel]].fold(
  errors => {
    println(errors)
  },
  data => {
    println(data.map(Json.toJson(_)).mkString("\n"))
  }
)

val json2 = Json.parse(
  """
     {"id":1,"name":"hello world","data":"{\"id\":2,\"name\":\"name2\"}"}
  """)
( json2 \ "data").validate[String].fold(
  errors=>{
    println(errors)
  },
  data=>{
    println(Json.parse(data).toString())
  }
)

def str2npram(s: String, start: Int = 1, end: Int = 3) = {
  (start to end).flatMap(m => (0 to s.length - m).map(n => s.substring(n, n + m)))
}

str2npram("我爱北京天安门")